﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace Common.Tool
{
    public class ListToString : IValueConverter
    {
        /// <summary>
        /// 将List<string>转换为string
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return "";

            if (value is string)
            {
                return value.ToString();
            }
            else if (value is List<string>)
            {
                List<string> strList = new List<string>();
                string sep = parameter == null ? "\r\n" : parameter.ToString();

                foreach (var item in value as List<string>)
                {
                    strList.Add(item?.ToString());
                }

                return string.Join(sep, strList);
            }

            return "";
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}
